package com.wdh.testq;

import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import com.bumptech.glide.Glide;
import com.wdh.testq.dialog.BaseDialog;
import com.wdh.testq.dialog.ChoosePictureDialog;
import com.wdh.testq.dialog.PermissionDialog;
import com.wdh.testq.tools.CallBack;
import com.wdh.testq.tools.PermissionTools;
import com.wdh.testq.tools.PhotoUtils;
import com.wdh.testq.tools.Resultful;
import com.werb.pickphotoview.PickPhotoView;
import com.werb.pickphotoview.util.PickConfig;

import java.io.File;
import java.util.List;

/**
 * androidQ照片选择  拍照
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView iv1, iv2;
    private final int PHOTO_REQUEST_TAKEPHOTO = 1; // 拍照
    private final int PHOTO_REQUEST_GALLERY = 2;   // 从相册中选择
    private final int PHOTO_REQUEST_CUT = 3;       // 结果

    //图片
    private String avatarPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        iv1 = findViewById(R.id.iv1);
        iv2 = findViewById(R.id.iv2);
        iv1.setOnClickListener(this);
        iv2.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv1:
                checkPhotoPermission();
                break;
            case R.id.iv2:
                //选择头像
                new ChoosePictureDialog(this).Build().setOnViewClickListener(new BaseDialog.OnViewClickListener() {
                    @Override
                    public void onClick(View view) {
                        switch (view.getId()) {
                            case R.id.tv_camera:
                                PhotoUtils.takePicture(MainActivity.this, PHOTO_REQUEST_TAKEPHOTO);
                                break;
                            case R.id.tv_photo:
                                PhotoUtils.openPic(MainActivity.this, PHOTO_REQUEST_GALLERY);
                                break;
                            default:
                                break;
                        }
                    }
                }).show();
                break;
            default:
                break;
        }
    }

    public void checkPhotoPermission() {
        PermissionTools.getInstance()
                .bind(this)
                .permission(PermissionTools.READ_EXTERNAL_STORAGE, PermissionTools.CHECK_CAMERA)
                .call(new CallBack() {
                    @Override
                    protected void onAction(List<Resultful> data) {
                        if (data == null)
                            popPhoto(true);
                        else {
                            for (Resultful rf : data) {
                                if (rf.getCode().equals(PermissionTools.READ_EXTERNAL_STORAGE) && !rf.isAllow()) {
                                    PermissionDialog dialog = new PermissionDialog(MainActivity.this).Build();
                                    dialog.setPermissionList(PermissionDialog.PermissionType.READ_EXTERNAL_STORAGE)
                                            .show();
                                    return;
                                } else if (rf.getCode().equals(PermissionTools.CHECK_CAMERA)) {
                                    popPhoto(rf.isAllow());
                                    return;
                                }
                            }
                            popPhoto(false);
                        }
                    }
                }).excute();
    }

    private void popPhoto(boolean b) {
        new PickPhotoView.Builder(this)
                .setPickPhotoSize(1)
                .setShowCamera(b)
                .setSpanCount(4)
                .setLightStatusBar(true)
                .setStatusBarColor("#ffffff")
                .setToolbarColor("#ffffff")
                .setToolbarIconColor("#000000")
                .setClickSelectable(false)
                .start();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PickConfig.PICK_PHOTO_DATA) {//选择照片返回
            if (data == null) {
                return;
            }
            List<String> selectPaths = (List<String>) data.getSerializableExtra(PickConfig.INTENT_IMG_LIST_SELECT);
            Glide.with(this).load(selectPaths.get(0)).into(iv1);
        }
        Log.i("aaa", "PHOTO_REQUEST_GALLERY:" + requestCode+"   "+resultCode);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case PHOTO_REQUEST_TAKEPHOTO://当选择拍照时调用
                PhotoUtils.cropImageUri(this, 1, 1, PHOTO_REQUEST_CUT);
                break;
            case PHOTO_REQUEST_GALLERY://当选择从本地获取图片时
                //做非空判断，当我们觉得不满意想重新剪裁的时候便不会报异常
                if (data != null) {
                    Uri newUri =  data.getData();
                    Log.i("aaa", "Y:" + newUri);
                    PhotoUtils.cropImageUri(this, 1, 1, newUri, PHOTO_REQUEST_CUT);
                }
                break;
            case PHOTO_REQUEST_CUT://返回的结果
                avatarPath = PhotoUtils.getCropPath();
                Log.i("aaa", "avatarPath:" +avatarPath);
                if (avatarPath == null) {
                    return;
                }
                Glide.with(this)
                        .load(new File(avatarPath))
                        .into(iv2);
                break;
        }
    }

}